<template>
  <div>
    <div class="header">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        size="mini"
        style="margin-left:2%;margin-top:1%;"
      >
        <el-form-item label="姓名">
          <el-input
            v-model="formInline.name"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="formInline.sex" placeholder="请选择性别"  clearable>
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="毕业年级">
          <el-input
            v-model="formInline.graduate"
            placeholder="请输入毕业年级(例如：18)"
          ></el-input>
        </el-form-item>
        <el-form-item label="高校">
          <el-input
            v-model="formInline.college"
            placeholder="请输入高校"
          ></el-input>
        </el-form-item>
        <el-form-item label="省份">
          <el-input
            v-model="formInline.province"
            placeholder="请输入省份"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="query" size="small">查询</el-button>
          <el-button type="primary" @click="getExcel" size="small"
            >导出</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="main">
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        height="550"
        border
        :default-sort = "{prop: 'graduate', order: 'descending'}"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="name" label="姓名" width="200" sortable>
        </el-table-column>
        <el-table-column prop="sex" label="性别" width="120"> </el-table-column>
        <el-table-column prop="graduate" label="毕业年级" width="180" sortable>
        </el-table-column>
        <el-table-column prop="phone" label="电话" width="250">
        </el-table-column>
        <el-table-column prop="college" label="高校" width="200">
        </el-table-column>
        <el-table-column prop="downtown" label="市区" width="150">
        </el-table-column>
        <el-table-column prop="province" label="省份" width="200">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>

export default {
  name: "exportData",
  data() {
    return {
      formInline: {
        name: "",
        sex: "",
        graduate: "",
        college: "",
        province: ""
      },
      tableData: [],
      multipleSelection: [],
      loading:false
    };
  },

  created() {},

  methods: {
    query() {
      this.loading = true;
      let email = localStorage.getItem("email");
      let { name, sex, graduate, college, province } = this.formInline;
      name = name.trim();
      graduate = graduate.trim();
      college = college.trim();
      province = province.trim();

      let str = "";
      if (name) {
        str = str + "&name=" + name;
      }
      if (graduate) {
        str = str + "&graduate=" + graduate;
      }
      if (college) {
        str = str + "&college=" + college;
      }
      if (province) {
        str = str + "&province=" + province;
      }
      if (sex) {
        str = str + "&sex=" + sex;
      }

      console.log(str);

      this.$axios
        .get("http://119.91.81.28:1337/students?createdBy=" + email + str)
        .then(
          res => {
            console.log(res);
            this.tableData = [];
            res.data.forEach(item => {
              this.tableData.push(item);
            });

            this.loading = false;
          },
          err => {
            console.log(err);
          }
        );

    },

    getExcel() {
      require.ensure([], () => {
        const {
          export_json_to_excel
        } = require("../utils/excel/Export2Excel.js");
        const tHeader = [
          "姓名",
          "性别",
          "毕业年级",
          "电话",
          "学校",
          "市区",
          "省份"
        ];
        const filterVal = [
          "name",
          "sex",
          "graduate",
          "phone",
          "college",
          "downtown",
          "province"
        ];
        const list = this.multipleSelection;
        const data = this.formatJson(filterVal, list);
        this.$prompt("请输入", "添加导出excel文件名", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        }).then(res => {
          export_json_to_excel(tHeader, data, res.value);
        });
      });
    },

    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>

<style>
.header {
  width: 100%;
  height: 10vh;
  background-color: #c0c0c0;
  display: flex;
  align-items: center;
}
.main {
  width: 100%;
  height: 87vh;
}
</style>
